package com.lin.musicplayer;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.lin.musicplayer.db.MusicDB;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by crystal on 2016/7/8.
 */
 public class ListShow extends Activity{

    private SQLiteDatabase dbRead,dbWrite;
    private MusicDB db;



    String mylist;
    private MediaPlayer mediaPlayer;
    private static final File Music_Path=new File("/sdcard/mp3/");
    public List<String> musiclist;
    ListView listView;
    String songname;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addlist);
        Intent y=this.getIntent();
        Bundle bundle=this.getIntent().getExtras();
        mylist=bundle.getString("list");
        db = new MusicDB(this);
        dbRead = db.getReadableDatabase();
        dbWrite = db.getWritableDatabase();

        listView=(ListView)findViewById(R.id.addlistshow);
        setListAdapter();
        listView.setOnItemClickListener(new ItemClickEvent());

    }
    public int saveMusic() {
        ContentValues cv = new ContentValues();
        cv.put("name", songname);
        return (int) dbWrite.insert(mylist, null, cv);
    }
    public ListShow()
    {
        musiclist=new ArrayList<String>();
        if(Music_Path.listFiles(new MusicFilter()).length>0)
        {
            for(File file:Music_Path.listFiles(new MusicFilter()))
            {
                musiclist.add(file.getAbsolutePath());
            }
        }
    }
    public void setListAdapter()
    {
        List<Map<String,Object>> date=new ArrayList<Map<String,Object>>();
        for(String path:musiclist)
        {
            Map<String,Object>map=new HashMap<String, Object>();
            File file=new File(path);
            map.put("fileName",file.getName());
            date.add(map);
            SimpleAdapter adapter=new SimpleAdapter(this,date,android.R.layout.simple_list_item_2,new String[]{"fileName"},new int[]{android.R.id.text1});
            listView.setAdapter(adapter);
        }
    }
    private final class ItemClickEvent implements AdapterView.OnItemClickListener {

        Map<String,Object>map=new HashMap<String, Object>();
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            map= (Map<String, Object>) listView.getItemAtPosition(i);
            String text= (String) map.get("fileName");
            songname=text;
            if(saveMusic()!=-1){
                Toast.makeText(ListShow.this,"收藏成功",Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(ListShow.this,"收藏失败",Toast.LENGTH_SHORT).show();
            }

        }
    }

}
